package com.translationanimate;

import android.os.Bundle;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
	Button bt1;
	int valuex = 0, valuey;
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		
		Log.i("bt1.getX()", ""+bt1.getX());
		Log.i("bt1.getY()", ""+bt1.getY());


	}
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		bt1 = (Button) findViewById(R.id.button1);

		Button dos = (Button) findViewById(R.id.button2);
		dos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				moveObject(bt1, "down");
				
			}
		});
		Button tres = (Button) findViewById(R.id.button3);
		tres.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				moveObject(bt1, "left");
				
			}
		});
		Button cuatro = (Button) findViewById(R.id.button4);
		cuatro.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				moveObject(bt1, "right");
				
			}
		});
		
		
		bt1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				moveObject(v, "up");
			}
		});
		
		
		
		
	}
	
	

	protected void moveObject(View v, String string) {
		if(string.equals("up")){
			ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationY", valuey, valuey-100);
			anim.setDuration(1000);
			anim.start();
			valuey = valuey -100;	
		}else if(string.equals("down")){
			ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationY", valuey, valuey+100);
			anim.setDuration(1000);
			anim.start();
			valuey = valuey +100;	
		}else if(string.equals("left")){
			ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationX", valuex, valuex-100);
			anim.setDuration(1000);
			anim.start();
			valuex = valuex -100;	
		}else if(string.equals("right")){
			ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationX", valuex, valuex+100);
			anim.setDuration(1000);
			anim.start();
			valuex = valuex +100;	
		}
	
	}



	protected void animationMethod(View v) {
		Log.i("metodo", "metodo");
		Log.i("v getx", ""+v.getX());
		Log.i("v gety", ""+v.getY());

		Integer entero = (Integer.parseInt(v.getTag().toString()));
		Log.i("tag", ""+v.getTag());
//		ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationX",(int)entero, 100);
		ObjectAnimator anim = ObjectAnimator.ofFloat(v, "translationX", 100,(int)entero);

		anim.setDuration(1000);
		anim.start();
		v.setTag(entero + 100);
	}



	protected void moveView(final View view, final RelativeLayout.LayoutParams layoutParams, float x, float y) {
		
		
		TranslateAnimation transAnimation= new TranslateAnimation(0, 100, 0, 100);
		 
		//fromXposition- x coordinate from  where animation should start
		//toXPosition- x coordinate at which animation would end
		//fromYPosition- y coordinate from where animation should start.
		//toYPosition- y coordinate at which animation would end.
		 
		
		transAnimation.setFillAfter(true);
		//You can also set duration for the animation that means you can set for how long the animation should last:
		transAnimation.setDuration(1000);
		
		transAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				layoutParams.topMargin = layoutParams.topMargin + 100;
				layoutParams.leftMargin = layoutParams.leftMargin + 100;
				view.setLayoutParams(layoutParams);

				
			}
		});
		
		//You can now apply the animation to a view
		view.startAnimation(transAnimation);
		
		
		
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
